﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

namespace TestApp
{
    class Program
    {

        //static string sqlDependency = "SELECT a.SID ,SNAME,SAGE,convert(nvarchar(10),sdate,121) dat  FROM dbo.TA as a inner join dbo.TB as b on a.sid=b.sid where a.sdate=@sdate  order by SID";
        //static string sqlDependency = "SELECT a.SID ,SNAME,SAGE,convert(nvarchar(10),sdate,121) dat  FROM dbo.TA as a inner join dbo.TB as b on a.sid=b.sid   order by SID";
        //static string sqlDependency = "SELECT TA.SID ,SNAME,SAGE, CID, CNAME FROM TA LEFT JOIN TB ON TA.SID=TB.SID";
        static string sqlDependency = "SELECT SID, SName ,SDate from dbo.TA where sid in (1,2,3,5,6,7) ";
        static string connectionString = "Data Source=.; Initial Catalog=scc; User ID= sa; Password=sa;";
        
        static void MainP(string[] args)
        {
            //string sqlDependency = "SELECT TA.SID ,SNAME,SAGE, CID, CNAME FROM TA LEFT JOIN TB ON TA.SID=TB.SID";
            SqlDependency.Start(connectionString);


            List<SqlParameter> sqlParamList = new List<SqlParameter>();
            SqlParameter param1 = new SqlParameter("@sdate", SqlDbType.DateTime);
            param1.Value = Convert.ToDateTime("2011-12-12");
            sqlParamList.Add(param1);

            SqlDependencyData data = new SqlDependencyData(connectionString, sqlDependency,null, delegate(DataTable dt)
            {
                if (dt.Rows.Count > 0)
                {
                    Console.WriteLine("SID\tSNAME\tSAGE");
                    //Console.WriteLine("SID\tSNAME\tSAGE\tCID\tCNAME");
                    foreach (DataRow dr in dt.Rows)
                    {
                        Console.WriteLine(string.Format("{0}\t{1}\t{2}", dr[0], dr[1], dr[2]));
                        //Console.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}", dr[0], dr[1], dr[2],dr[3],dr[4]));
                    }
                }
            });
            //SqlDependencyData data = new SqlDependencyData(connectionString, sqlDependency, null, null);
            data.CheckChange( );

            Console.ReadLine();

            

            SqlDependency.Stop(connectionString);
        }
    }
}
